import AdapterHelper from "./engine/AdapterHelper";
import ComponentBase from "./engine/ComponentBase";
import { CryptoTSHelper } from "./engine/CryptoTSHelper";
import DateUtil from "./engine/DateUtil";
import { EngineConfig } from "./engine/EngineConfig";
import { EventManager } from "./engine/EventManager";
import { Logger } from "./engine/Logger";
import QRCodeHelper from "./engine/QRCodeHelper";
import { ScreenShotHelper } from "./engine/ScreenShotHelper";
import UIManager from "./engine/UIManager";
import { WebUtil } from "./engine/WebUtil";
import { WebViewPrefab } from "./engine/WebViewPrefab";
import { ViewConfig } from "./game/config/ViewConfig";

import GameController from "./GameController";
import { ProtoManager } from "./lib/net/protobuf/ProtoManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Main extends ComponentBase {
    public static instance: Main;

    public widthRate: number

    onLoad() {
        WebUtil.init()
        Main.instance = this;
        UIManager.instance.init(this.getChild(this.node, "uiRoot"))
        ScreenShotHelper.init(this.getChild(this.node, "captureCamera", cc.Camera))
        AdapterHelper.fixApdater();
        GameController.instance.init(this);
        ProtoManager.instance.init()
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyUp, this);
        // UIManager.instance.showView(ViewConfig.viewConfig.ViewLoading)

        // Logger.log("aee=")
        // HttpHelper.test()
        // this.scaleScreen()

        // ErrorCollection.instance.initCollectError()

        // this.scheduleOnce(()=>{
        //     Logger.log("", a.abcd)
        // }, 2)

        // let abce:string = "abcd_eft_ddd"
        // let encrypt:string = CryptoTSHelper.encrypt(abce);
        // Logger.log("encrypt==", encrypt)
        // let result = CryptoTSHelper.decrypt(encrypt)

        let test: cc.Node = this.findChild(this.node, "test")
        QRCodeHelper.drawQRCode(test,
            "http://www.baidu.com")

    }


    start(): void {
        // WebViewPrefab.instance.show("http://119.23.247.29:13000/juyou")
    }

    private onKeyUp(event) {
        // UIHelper.showTip("keyCode:" + event.keyCode)
        // 下:1004
        //上:1003
        //左:1000
        //右:1001
        //确定:1005
        //返回：6
        //菜单:18
        if (event.keyCode == cc.macro.KEY.back) {

        }
    }

    private scaleScreen() {
        let size: cc.Size = cc.view.getVisibleSize();
        let designsize = cc.view.getDesignResolutionSize();
        let width: number = size.width;
        let height: number = size.height;
        let lw: number = width / designsize.width;
        let lh: number = height / designsize.height;
        Logger.log("scaleScreen===", size.width, size.height, designsize.width, designsize.height)
        Logger.log("scaleScreen==11=", lw)
        Logger.log("scaleScreen==22=", lh)
        this.node.scaleX = lw
        this.node.scaleY = lh
        this.widthRate = lw;
    }

    private onDispatchKeyUp(keyType: number) {
        // EventManager.instance.dispatchOnce(GameConst.Event_Type.Event_Key_Up, keyType)
    }

    update(dt) {
        EventManager.instance.dispatch(EngineConfig.Event_Type.Event_FrameUpdate, DateUtil.now());
    }



}
